package com.yc.common.secure.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yc.common.utils.JsonUtils;
import com.yc.common.utils.ServletUtil;
import com.yc.entity.User;
import com.yc.vo.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @program: model_project
 * @description: 登录成功处理
 * @author: 作者 huchaojie
 * @create: 2023-06-13 16:50
 */
@Component
public class SecureAuthenticationSuccessHandler implements AuthenticationSuccessHandler {


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication a) throws IOException {
        Result result = Result.success("登录成功");
        //身份信息
        JSONObject json = (JSONObject) JSONObject.toJSON(a.getPrincipal());
        Map<String, Object> data = new HashMap<>(5);
        User user = (User) request.getSession().getAttribute("user");

        if(json.getString("rolecode").contains("STUDENT")){
            //学生
            data.put("identity", 0);
            result.setData(data);
        }else if (json.getString("rolecode").contains("ADMIN")){
            //超级管理员
            data.put("identity", 1);
            data.put("nickname", json.getString("nickname"));
            data.put("username", ((User) request.getSession().getAttribute("user")).getUsername());
            data.put("id", user.getId());
            result.setData(data);
        } else {
            if (json.getString("rolecode").contains("LEADER") && json.getString("rolecode").contains("TEACHER")&&json.getString("rolecode").contains("COUNSELLOR")) {
                // 即是领导 也是老师
                data.put("identity", 4);
                data.put("nickname", json.getString("nickname"));
                data.put("username", ((User) request.getSession().getAttribute("user")).getUsername());
                data.put("id", user.getId());
                result.setData(data);
            } else if (json.getString("rolecode").contains("LEADER") && json.getString("rolecode").contains("TEACHER")) {
                // 即是领导 也是老师
                data.put("identity", 4);
                data.put("nickname", json.getString("nickname"));
                data.put("username", ((User) request.getSession().getAttribute("user")).getUsername());
                data.put("id", user.getId());
                result.setData(data);
            } else if (json.getString("rolecode").contains("LEADER")) {
                //领导 1
                data.put("identity", 1);
                data.put("nickname", json.getString("nickname"));
                data.put("id", json.getString("id"));
                data.put("username", ((User) request.getSession().getAttribute("user")).getUsername());
                result.setData(data);
            } else if (json.getString("rolecode").contains("TEACHER")) {
                //老师
                data.put("identity", 3);
                data.put("nickname", json.getString("nickname"));
                data.put("username", ((User)request.getSession().getAttribute("user")).getUsername());
                data.put("id", json.getString("id"));
                result.setData(data);
            } else if (json.getString("rolecode").contains("COUNSELLOR")) {
                //辅导员
                data.put("identity", 2);
                data.put("username", ((User) request.getSession().getAttribute("user")).getUsername());
                data.put("id", json.getString("id"));
                data.put("nickname", json.getString("nickname"));
                result.setData(data);
            } else {
                result = Result.success("没有账号");
            }
        }
        ServletUtil.write(JSON.toJSONString(result));
    }
}
